package org.logMonitor.web;

import net.sf.json.JSONArray;
import org.logMonitor.dto.IpSort;
import org.logMonitor.dto.IpTrend;
import org.logMonitor.entity.Record;
import org.logMonitor.entity.Rule;
import org.logMonitor.service.IpSortService;
import org.logMonitor.service.RecordService;
import org.logMonitor.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by lenovo on 2018/1/19.
 */
@Controller
@RequestMapping("/ipSort")//url:模块/资源/{}/细分
public class IpSortController {
    @Autowired
    private IpSortService ipSortService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private RuleService ruleService;

    /**
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public  String list (Model model){
        List<IpSort> list=ipSortService.getIpSortFromRedis();
        model.addAttribute("list",list);
        return "ipSortList";
    }

    /**
     * 展示ip总流量排名前5名，利用饼图
     * @return
     */
    @RequestMapping(value = "/ipSortInfo",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    //自动转为json
    @ResponseBody
    public List<IpSort> exposer(){
        List<IpSort> ipSortList = new ArrayList<IpSort>();
        ipSortList = ipSortService.getIpSortFromRedis();
        return  ipSortList;
    }

    /**
     * 展示ip趋势图
     * @return
     */
    @RequestMapping(value = "/ipTrend",method = RequestMethod.POST)
    public String ipTrend(@RequestParam("ruleId") int ruleId, @RequestParam("ipType") int ipType,@RequestParam("srcDstIp") String srcDstIp,Model model){
        System.out.println(ruleId+ipType+srcDstIp);
        Rule rule = ruleService.getRuleById(ruleId);
        srcDstIp = srcDstIp.split(":")[1];
        List<Record> recordList = new ArrayList<Record>();
        List<String> recordTimeList = new ArrayList<String>();
        List<Integer> recordValueList = new ArrayList<Integer>();
        if(ipType == 1){
            recordList = recordService.getRecordByIdDstip(ruleId,srcDstIp,0,7);
        }else if(ipType == 0){
            recordList = recordService.getRecordByIdSrcip(ruleId,srcDstIp,0,7);
        }else{
            return null;
        }
        Collections.reverse(recordList);//把得到的list顺序反转
        for(Record record : recordList){
//            recordTimeList.add(record.getCreatTime().split(" ")[1]);
            recordTimeList.add(record.getCreatTime());
            recordValueList.add(record.getValue());
        }
        String jsonDocumentList = JSONArray.fromObject(recordTimeList).toString();
        model.addAttribute("recordTimeList",recordTimeList);
        model.addAttribute("recordValueList",recordValueList);
        model.addAttribute("documentList", jsonDocumentList);
        model.addAttribute("ruleId", ruleId);
        model.addAttribute("ipType", ipType);
        model.addAttribute("srcDstIp", srcDstIp);
        model.addAttribute("rule",rule);
        return "ipTrend";
    }

    @RequestMapping(value = "/ipTrendInfo",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    //自动转为json
    @ResponseBody
    public IpTrend getChinaMapInfo(int ruleId,int ipType,String srcDstIp){
        IpTrend ipTrend = new IpTrend();
        List<Record> recordList = new ArrayList<Record>();
        List<String> recordTimeList = new ArrayList<String>();
        List<Integer> recordValueList = new ArrayList<Integer>();
        if(ipType == 1){
            recordList = recordService.getRecordByIdDstip(ruleId,srcDstIp,0,7);
        }else if(ipType == 0){
            recordList = recordService.getRecordByIdSrcip(ruleId,srcDstIp,0,7);
        }else{
            return null;
        }
        Collections.reverse(recordList);//把得到的list顺序反转
        for(Record record : recordList){
//            recordTimeList.add(record.getCreatTime().split(" ")[1]);
            recordTimeList.add(record.getCreatTime());
            recordValueList.add(record.getValue());
        }
        ipTrend.setRecordTimeList(recordTimeList);
        ipTrend.setRecordValueList(recordValueList);
        return  ipTrend;
    }


    @RequestMapping(value = "/tcpUdplist",method = RequestMethod.GET)
    public  String tcpUdplist (Model model){

        return "tcpUdp";
    }
    @RequestMapping(value = "/ipTcpUdpInfo",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    //自动转为json
    @ResponseBody
    public List<IpSort> tcpUdpExposer(){
        List<IpSort> ipSortList = new ArrayList<IpSort>();
        ipSortList = ipSortService.getTcpUdpFromRedis();
        return  ipSortList;
    }
}
